package com.youi.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.entity.MarketTheme;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.service.MarketThemeService;
import com.youi.util.CommonInfoUtil;
import com.youi.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @Author: winty
 * @Date: 2020/5/15 9:14
 */
@RestController
public class MarketThemeController extends HttpRequestManage {

    @Autowired
    private MarketThemeService marketThemeService;

    /**
     * 获取首页专题列表(12个位置)
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/listMarketTheme")
    public String listMarketTheme(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        Map<String, Object> params = Maps.newHashMap();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put(Common.STATUS, 1);
        params.put("position", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));

        Map<String, Object> gtParams = Maps.newHashMap();
        gtParams.put("gmtEnd", new Date());

        Map<String, Object> ltParams = Maps.newHashMap();
        ltParams.put("gmtBegin", new Date());

        List<MarketTheme> themeList = marketThemeService.listMarketThemeByParam(params, null, gtParams, ltParams);
        if (CollectionUtils.isEmpty(themeList)) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        //如果某个位置设置了2个或2个以上的专题，只取其中一个
        Map<Integer, MarketTheme> resultMap = themeList.stream().collect(Collectors.groupingBy(
                MarketTheme::getPosition, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))));

        //返回新的结果集
        List<MarketTheme> resultList = Lists.newArrayList();
        resultMap.forEach((k, v) -> {
            resultList.add(v);
        });

        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", resultList);
    }

    /**
     * 获取专题详情
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getMarketThemeById")
    public String getMarketThemeById(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        Long id = optLong("id", request);

        MarketTheme marketTheme = marketThemeService.getMarketThemeById(id);
        if (marketTheme == null) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", marketTheme);
    }
}
